package com.tt.payee.controller.reptile;

import com.tt.payee.api.ResponseResult;
import com.tt.payee.service.reptile.ProductInformationService;
import com.tt.payee.utils.file.BASE64DecodedMultipartFile;
import com.tt.payee.utils.file.QiniuCloudUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.tt.payee.utils.file.ImageUtils.image2Base64;

/**
 * @author ：Tang
 * @date ：Created in 2020/8/21 15:18
 * @description：
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping("/productInformationController")
@Slf4j
public class ProductInformationController {

    @Autowired
    ProductInformationService productInformationService;

    /**
     * ABB
     *
     * @return
     */
    @PostMapping("/abbProductInformation")
    public ResponseResult<Object> abbProductInformation() {
        //1.生成httpclient，相当于该打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //2.创建get请求，相当于在浏览器地址栏输入 网址
        HttpGet request = new HttpGet("http://abbdianqi.56tv.com.cn/sell/view-1-typeid-0.shtml");

        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36");

        //3.执行get请求，相当于在输入地址栏后敲回车键
        try {
            response = httpClient.execute(request);

            //5.获取响应内容
            HttpEntity httpEntity = response.getEntity();
            String html = EntityUtils.toString(httpEntity, "utf-8");

            //6.Jsoup解析html
            Document document = Jsoup.parse(html);
            Element pages = document.getElementsByClass("pages").first();

            //获取页面
            int page = Integer.parseInt(pages.getElementsByTag("span").text());

            for (int i = 1; i <= page; i++) {
                //1.生成httpclient，相当于该打开一个浏览器
                CloseableHttpClient httpClient2 = HttpClients.createDefault();
                CloseableHttpResponse response2 = null;

                //2.创建get请求，相当于在浏览器地址栏输入 网址
                HttpGet request2 = new HttpGet("http://abbdianqi.56tv.com.cn/sell/view-1-page-" + i + ".shtml");

                request2.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36");

                response2 = httpClient.execute(request2);


                HttpEntity httpEntity2 = response2.getEntity();
                String html2 = EntityUtils.toString(httpEntity2, "utf-8");


                Document document2 = Jsoup.parse(html2);
                Element pages2 = document2.getElementsByClass("pages").first();

                Elements productList = document2.getElementsByClass("lh18");

                //遍历列表
                for (int a = 1; a < productList.size(); a++) {
                    Element product = productList.get(a);

                    Element first = product.getElementsByTag("a").first();

                    System.out.println("页面产品" + a + "asdd" + first.attr("src"));

                    String url = first.attr("href");

                    Thread.sleep(500);

                    //获取单个页面的数据
                    abbHtml(url);
                }
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            //6.关闭
            HttpClientUtils.closeQuietly(response);
            HttpClientUtils.closeQuietly(httpClient);
        }

        return ResponseResult.success("获取ABB官网商品成功");
    }

    /**
     * 添加abb
     *
     * @param url
     */
    public void abbHtml(String url) {
        //1.生成httpclient，相当于该打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //2.创建get请求，相当于在浏览器地址栏输入 网址
        HttpGet request = new HttpGet(url);

        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36");

        try {
            //3.执行get请求，相当于在输入地址栏后敲回车键
            response = httpClient.execute(request);

            //4.判断响应状态为200，进行处理
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //5.获取响应内容
                HttpEntity httpEntity = response.getEntity();
                String html = EntityUtils.toString(httpEntity, "utf-8");

                //6.Jsoup解析html
                Document document = Jsoup.parse(html);

                //抓取名称
                Element fr = document.getElementById("hits");

                System.out.println("产品名称：" + fr.parent().getElementsByTag("strong").text());

                String name = fr.parent().getElementsByTag("strong").text();


                //生成wrod文档
                XWPFDocument word = new XWPFDocument();

                //Write the Document in file system
                FileOutputStream out = new FileOutputStream(new File("C:/Users/Dell/Desktop/产品wrod/" + name + ".docx"));

                //添加标题
//                XWPFParagraph titleParagraph = word.createParagraph();

                //设置段落居中
//                titleParagraph.setAlignment(ParagraphAlignment.CENTER);
//
//                XWPFRun titleParagraphRun = titleParagraph.createRun();
//                titleParagraphRun.setText(name);
//                titleParagraphRun.setColor("000000");
//                titleParagraphRun.setFontSize(20);
//                titleParagraphRun.setFontFamily("宋体");

                Element r0 = document.getElementById("t_0");

                Elements parent1 = r0.parent().getElementsByTag("img");

                StringBuffer urls = new StringBuffer();

                //抓取产品图片
                for (Element parent : parent1) {

                    if (parent.attr("src").equals("http://www.56tv.com.cn/skin/defaulthsb06/image/nopic60.gif")) {
                        break;
                    }

                    URL wordurl = null;

                    InputStream is = null;

                    HttpURLConnection httpUrl = null;

                    wordurl = new URL(parent.attr("src"));

                    httpUrl = (HttpURLConnection) wordurl.openConnection();

                    httpUrl.connect();

                    httpUrl.getInputStream();

                    is = httpUrl.getInputStream();


                    //word段落
                    XWPFParagraph firstParagraph = word.createParagraph();

                    firstParagraph.getStyleID();

                    firstParagraph.setStyle("Heading1");
                    //设置段落居中
                    firstParagraph.setAlignment(ParagraphAlignment.CENTER);

                    XWPFRun run = firstParagraph.createRun();

                    run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, parent.attr("src"), Units.toEMU(200), Units.toEMU(200));
                }

                //抓取品牌
                Elements fDblue = document.getElementsByClass("f_dblue");

                Element element = fDblue.get(1);

                System.out.println("产品品牌：" + element.parent().getElementsByTag("td").last().text());

                String brand = element.parent().getElementsByTag("td").last().text();

                //抓取产品介绍
                Element content = document.getElementById("content");

                Elements allElements = content.children();

//                Elements allElements = content.getElementsByTag("div");

                StringBuilder text = new StringBuilder();

                for (Element allElement : allElements) {

                    Elements ps = allElement.getElementsByTag("p");

                    text.append(allElement.text()).append("/n");

                    //word段落
                    XWPFParagraph firstParagraph = word.createParagraph();

                    firstParagraph.getStyleID();

                    firstParagraph.setStyle("Heading1");

                    XWPFRun run = firstParagraph.createRun();

                    run.setText(allElement.text());

                    run.setColor("000000");
                    run.setFontFamily("宋体");
                    run.setFontSize(8);
                }


                word.write(out);

                //写入数据库
//                ProductInformation productInformation = new ProductInformation();
//
//                productInformation.setProductName(name);
//                productInformation.setProductBrand(brand);
//                productInformation.setProductIntroduce(text.toString());
//                productInformation.setProductUrl(urls.toString());
//                productInformation.setCreated(new Date());
//                productInformation.setUpdated(new Date());
//
//                int i = productInformationService.addProductInformation(productInformation);

//                if (i == Constant.LOSE) {
//                    throw new BusinessException("上传数据库失败");
//                }

            } else {
                System.out.println("返回状态不是200");
                System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        } finally {
            //6.关闭
            HttpClientUtils.closeQuietly(response);
            HttpClientUtils.closeQuietly(httpClient);
        }
    }


    /**
     * 工博士
     *
     * @return
     */
    @GetMapping("/gbsProductInformation")
    public ResponseResult<Object> gbsProductInformation() {
        //1.生成httpclient，相当于该打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //2.创建get请求，相当于在浏览器地址栏输入 网址
        HttpGet request = new HttpGet("http://www.siemens-mro.gongboshi.com//sell/index.php?view=1&page=1");

        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36");

        //3.执行get请求，相当于在输入地址栏后敲回车键
        try {
            response = httpClient.execute(request);

            //5.获取响应内容
            HttpEntity httpEntity = response.getEntity();
            String html = EntityUtils.toString(httpEntity, "utf-8");

            //6.Jsoup解析html
            Document document = Jsoup.parse(html);
            Element pages = document.getElementsByClass("pages").first();

            //获取页面
            int page = Integer.parseInt(pages.getElementsByTag("span").text());

            for (int i = 1; i <= page; i++) {
                //1.生成httpclient，相当于该打开一个浏览器
                CloseableHttpClient httpClient2 = HttpClients.createDefault();
                CloseableHttpResponse response2 = null;

                //2.创建get请求，相当于在浏览器地址栏输入 网址
                HttpGet request2 = new HttpGet("http://www.siemens-mro.gongboshi.com//sell/index.php?view=1&page=" + i);

                request2.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36");

                response2 = httpClient.execute(request2);


                HttpEntity httpEntity2 = response2.getEntity();
                String html2 = EntityUtils.toString(httpEntity2, "utf-8");


                Document document2 = Jsoup.parse(html2);
                Element pages2 = document2.getElementsByClass("pages").first();

                Elements productList = document2.getElementsByClass("lh18");

                //遍历列表
                for (int a = 1; a < productList.size(); a++) {
                    Element product = productList.get(a);

                    Element first = product.getElementsByTag("a").first();

                    System.out.println("页面产品" + a + "asdd" + first.attr("src"));

                    String url = first.attr("href");

                    Thread.sleep(500);

                    //获取单个页面的数据
                    gbsHtml(url);
                }
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            //6.关闭
            HttpClientUtils.closeQuietly(response);
            HttpClientUtils.closeQuietly(httpClient);
        }

        return ResponseResult.success("获取ABB官网商品成功");
    }

    /**
     * gbs
     *
     * @param url
     */
    public void gbsHtml(String url) {
        //1.生成httpclient，相当于该打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //2.创建get请求，相当于在浏览器地址栏输入 网址
        HttpGet request = new HttpGet(url);

        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36");

        try {
            //3.执行get请求，相当于在输入地址栏后敲回车键
            response = httpClient.execute(request);

            //4.判断响应状态为200，进行处理
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //5.获取响应内容
                HttpEntity httpEntity = response.getEntity();
                String html = EntityUtils.toString(httpEntity, "utf-8");

                //6.Jsoup解析html
                Document document = Jsoup.parse(html);

                //抓取名称
                Element fr = document.getElementById("hits");

                System.out.println("产品名称：" + fr.parent().getElementsByTag("strong").text());

                String name = fr.parent().getElementsByTag("strong").text();



                //生成wrod文档

                XWPFDocument word = new XWPFDocument();

                //Write the Document in file system
                Pattern pattern = Pattern.compile("[\\s\\\\/:\\*\\?\\\"<>\\|]");

                Matcher matcher = pattern.matcher(name);

                name = matcher.replaceAll("");

                FileOutputStream out = new FileOutputStream(new File("C:/Users/Dell/Desktop/产品wrod/" + name + ".docx"));

                //添加标题
//                XWPFParagraph titleParagraph = word.createParagraph();

                //设置段落居中
//                titleParagraph.setAlignment(ParagraphAlignment.CENTER);
//
//                XWPFRun titleParagraphRun = titleParagraph.createRun();
//                titleParagraphRun.setText(name);
//                titleParagraphRun.setColor("000000");
//                titleParagraphRun.setFontSize(20);
//                titleParagraphRun.setFontFamily("宋体");

                //抓取品牌
                Elements fDblue = document.getElementsByClass("f_dblue");

                Element element = fDblue.get(1);

                System.out.println("产品品牌：" + element.parent().getElementsByTag("td").last().text());

                String brand = element.parent().getElementsByTag("td").last().text();

                //抓取产品图片
                Element r0 = document.getElementById("mid_div");

                Elements parent1 = r0.getElementsByTag("img");

                if (parent1.get(0) != null) {

                    URL wordurl = null;

                    InputStream is = null;

                    HttpURLConnection httpUrl = null;

                    wordurl = new URL(parent1.get(0).attr("src"));

                    httpUrl = (HttpURLConnection) wordurl.openConnection();

                    httpUrl.connect();

                    httpUrl.getInputStream();

                    is = httpUrl.getInputStream();


                    //word段落
                    XWPFParagraph firstParagraph = word.createParagraph();

                    firstParagraph.getStyleID();

                    firstParagraph.setStyle("Heading1");
                    //设置段落居中
                    firstParagraph.setAlignment(ParagraphAlignment.CENTER);

                    XWPFRun run = firstParagraph.createRun();

                    run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, parent1.get(0).attr("src"), Units.toEMU(200), Units.toEMU(200));

//                    urls.append(urlToQiniu(parent.attr("src"))).append(",");
//                    System.out.println(parent.attr("src"));
                }

                //抓取产品介绍
                Elements content = document.getElementsByClass("ProductDetailsTable");

                StringBuilder text = new StringBuilder();
                if (content.size() != 0) {
                    Elements allElements = content.get(0).getElementsByTag("tbody");

                    Elements childrens = allElements.get(0).children();

                    for (Element allElement : childrens) {
//                        text.append(allElement.text()).append("/n");

                        Elements ps = allElement.getElementsByTag("p");

//                        text.append(allElement.text()).append("/n");

                        //word段落
                        XWPFParagraph firstParagraph = word.createParagraph();

                        firstParagraph.getStyleID();

                        firstParagraph.setStyle("Heading1");

                        XWPFRun run = firstParagraph.createRun();

                        run.setText(allElement.text());

                        run.setColor("000000");
                        run.setFontFamily("宋体");
                        run.setFontSize(8);
                    }
                }

                word.write(out);

//                ProductInformation productInformation = new ProductInformation();
//
//                productInformation.setProductName(name);
//                productInformation.setProductBrand(brand);
//                productInformation.setProductIntroduce(text.toString());
//                productInformation.setProductUrl(urls.toString());
//                productInformation.setCreated(new Date());
//                productInformation.setUpdated(new Date());
//
//                System.out.println("产品名称:" + name);
//                System.out.println("产品品牌:" + brand);
//                System.out.println("产品介绍:" + text.toString());
//                System.out.println("产品图片:" + urls.toString());
//                System.out.println("当前时间:" + new Date());
//                System.out.println("当前时间:" + new Date());
//
//                int i = productInformationService.addProductInformation(productInformation);
//
//                if (i == Constant.LOSE) {
//                    throw new BusinessException("上传数据库失败");
//                }

            } else {
                System.out.println("返回状态不是200");
                System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        } finally {
            //6.关闭
            HttpClientUtils.closeQuietly(response);
            HttpClientUtils.closeQuietly(httpClient);
        }
    }

    /**
     * 根据网络地址传到七牛云商
     *
     * @return
     */
    public String urlToQiniu(String url) {
        String urlBase64 = image2Base64(url);

        MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(urlBase64);

        QiniuCloudUtil qiniuCloudUtil = new QiniuCloudUtil();

        return qiniuCloudUtil.qiNiuuploadFileOne(multipartFile);
    }
}
